package com.huasan.www.demon.data;

import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huasan.www.demon.data.service.AccountinfoService;
import com.huasan.www.demon.data.service.UserdetailService;
import com.huasan.www.demon.pojo.Accountinfo;
import com.huasan.www.demon.pojo.Userdetail;
import com.huasan.www.demon.pojo.UserdetailExample;
import com.huasan.www.demon.rest.util.Constants;
import com.huasan.www.demon.util.SpringContextHolder;

public class TimingService implements Runnable{
	
	private AccountinfoService accountinfoService = SpringContextHolder.getBean("accountinfoService");
	
	private UserdetailService userdetailService = SpringContextHolder.getBean("userdetailService");
	
	public boolean isClosed;

	public static final Logger logger = LoggerFactory.getLogger(TimingService.class);
	
	public void ScanUserDetail(){
		try{
			logger.info("Scanning time out users...........");
			Timestamp currentTime = new Timestamp(System.currentTimeMillis());
			UserdetailExample example = new UserdetailExample();
			UserdetailExample.Criteria criteria = example.createCriteria();
//			criteria.andStatusNotEqualTo("2");
//			criteria.andEndtimeIsNull();
			criteria.andDurationIsNull();
			List<Userdetail> list = userdetailService.selectByExample(example);
			for (Userdetail ud : list){
				Timestamp alive = ud.getAlivetime();
				if (alive!=null)
					if (currentTime.getTime() - alive.getTime() > Constants.OFF_TIME){
						try{
							freeAccount(ud.getAccountid());
						}catch (Exception e){
							logger.error(e.getMessage(), e);
						}
						closeDetail(ud, currentTime);
					}
			}
		}catch (Exception e){
			logger.error(e.getMessage(), e);
		}
	}
	
	private void closeDetail(Userdetail ud, Timestamp endtime) {
		if (ud.getChargetime()==null)
			ud.setChargetime(endtime);
		ud.setStatus("2");
		ud.setEndtime(endtime);
		Long durationMin = (long)Math.ceil((new Double(endtime.getTime() - ud.getChargetime().getTime()))/Constants.MIN_TIME);
		ud.setDuration(durationMin);
		ud.setComment("Auto closed cause time out");
		userdetailService.updateByPrimaryKey(ud);
	}

	private void freeAccount(Long accountid) {
		Accountinfo account = accountinfoService.selectByPrimaryKey(accountid);
		account.setCuruser(null);
		account.setStatus("0");
		accountinfoService.updateByPrimaryKey(account);
	}

	@Override
	public void run() {
		while(!isClosed){
			try {
				Thread.sleep(Constants.SCAN_TIME);
				ScanUserDetail();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				logger.error(e.getMessage(), e);
			}
		}
		logger.info("Scanning Closed.............");
	}
	
	public void stop(){
		isClosed = true;
	}
	
	public boolean isClosed() {
		return isClosed;
	}
	
	public static void main(String[] argv){
		Timestamp t = new Timestamp(System.currentTimeMillis());
		System.out.println(t.getTime());
		System.out.println((long)Math.ceil((new Double(12L - 10L))/Constants.MIN_TIME));
	}

}
